#!/usr/bin/python3.4
# -*- coding=utf-8 -*-

import re

def filetodict(file):
    Regex = []
	R1_Regex = re.compile(r'======*R1======*')
	Regex.append(R1_Regex)
	R2_Regex = re.compile(r'======*R2======*')
	Regex.append(R2_Regex)
	R3_Regex = re.compile(r'======*R3======*')
	Regex.append(R3_Regex)
	R4_Regex = re.compile(r'======*R4======*')
	Regex.append(R4_Regex)
	R5_Regex = re.compile(r'======*R5======*')
	Regex.append(R5_Regex)
	R6_Regex = re.compile(r'======*R6======*')
	Regex.append(R6_Regex)

	ASA1_Regex = re.compile(r'======*ASA1======*')
	Regex.append(ASA1_Regex)
	ASA1SYS_Regex = re.compile(r'======*ASA1/SYS======*')
	Regex.append(ASA1SYS_Regex)
	ASA1ADMIN_Regex = re.compile(r'======*ASA1/admin======*')
	Regex.append(ASA1ADMIN_Regex)
	ASA1C1_Regex = re.compile(r'======*ASA1/c1======*')
	Regex.append(ASA1C1_Regex)
	ASA1C2_Regex = re.compile(r'======*ASA1/c2======*')
	Regex.append(ASA1C2_Regex)

	ASA2_Regex = re.compile(r'======*ASA2======*')
	Regex.append(ASA2_Regex)
	ASA2SYS_Regex = re.compile(r'======*ASA2/SYS======*')
	Regex.append(ASA2SYS_Regex)
	ASA2ADMIN_Regex = re.compile(r'======*ASA2/admin======*')
	Regex.append(ASA2ADMIN_Regex)
	ASA2C1_Regex = re.compile(r'======*ASA2/c1======*')
	Regex.append(ASA2C1_Regex)
	ASA2C2_Regex = re.compile(r'======*ASA2/c2======*')
	Regex.append(ASA2C2_Regex)

	ASA3_Regex = re.compile(r'======*ASA3======*')
	Regex.append(ASA3_Regex)
	ASA3SYS_Regex = re.compile(r'======*ASA3/SYS======*')
	Regex.append(ASA3SYS_Regex)
	ASA3ADMIN_Regex = re.compile(r'======*ASA3/admin======*')
	Regex.append(ASA3ADMIN_Regex)
	ASA3C1_Regex = re.compile(r'======*ASA3/c1======*')
	Regex.append(ASA3C1_Regex)
	ASA3C2_Regex = re.compile(r'======*ASA3/c2======*')
	Regex.append(ASA3C2_Regex)

	ASA4_Regex = re.compile(r'======*ASA4======*')
	Regex.append(ASA4_Regex)
	ASA4SYS_Regex = re.compile(r'======*ASA4/SYS======*')
	Regex.append(ASA4SYS_Regex)
	ASA4ADMIN_Regex = re.compile(r'======*ASA4/admin======*')
	Regex.append(ASA4ADMIN_Regex)
	ASA4C1_Regex = re.compile(r'======*ASA4/c1======*')
	Regex.append(ASA4C1_Regex)
	ASA4C2_Regex = re.compile(r'======*ASA4/c2======*')
	Regex.append(ASA4C2_Regex)

	SW1_Regex = re.compile(r'======*SW1======*')
	Regex.append(SW1_Regex)
	SW2_Regex = re.compile(r'======*SW2======*')
	Regex.append(SW2_Regex)
	SW3_Regex = re.compile(r'======*SW3======*')
	Regex.append(SW3_Regex)
	SW4_Regex = re.compile(r'======*SW4======*')
	Regex.append(SW4_Regex)
	SW5_Regex = re.compile(r'======*SW5======*')
	Regex.append(SW5_Regex)
	SW6_Regex = re.compile(r'======*SW6======*')
	Regex.append(SW6_Regex)
	IPS_Regex = re.compile(r'======*IPS======*')
	Regex.append(IPS_Regex)

	checkedfile = open(file, 'r')
	checkedlines = checkedfile.readlines()
	locations = []
	for reg in Regex:
		linelocation = 0
		for line in checkedlines:
			if reg.match(line):
				locations.append(linelocation)
			linelocation = linelocation + 1
	length_of_location = len(locations)
#	print(locations)
#	print(length_of_location)
	deviceconfigdict = {}
	for configlocation in range(length_of_location):
#		print(configlocation)
#		print(length_of_location)
		confignameline = checkedlines[locations[configlocation]:locations[configlocation]+1][0].strip()
#		print(confignameline)
		devicename = ''
		for char in confignameline:
			Device_Regex = re.compile(r'[a-zA-Z/0-9]')
			if Device_Regex.match(char):
				devicename = devicename + char
		if configlocation < length_of_location - 1:
#			print(configlocation)
#			print(length_of_location)
#			nowlocation = locations[configlocation]
#			print(nowlocation)
#			nextlocation = locations[configlocation+1]
#			print(nextlocation)
			deviceconfiglst = checkedlines[locations[configlocation]:locations[configlocation+1]]
		else:
			deviceconfiglst = checkedlines[locations[configlocation]:]
#		print(devicename)
#		print(type(deviceconfig))
#		print(len(deviceconfig))
		deviceconfig = ''
		matched = 0
		incorrect = re.compile(r'.*##.*')
		for line in deviceconfiglst:
			if incorrect.match(line):
				matched = 1
			deviceconfig = deviceconfig + line
#		print(deviceconfiglst)
#		deviceconfigbit = deviceconfig.encode()
#		print(deviceconfigbit)
#		print(devicename)
		if matched == 1:
			deviceconfigdict[devicename] = deviceconfig
#		print(deviceconfigdict)
	tmpfile = file.split('/')
	filename = tmpfile[len(tmpfile) - 1]
	values = filename.split('_')
	deviceconfigdict['Rackno'] = values[0]
	deviceconfigdict['verno'] = values[1]
	deviceconfigdict['usrname'] = values[2]
	deviceconfigdict['year'] = values[3]
	deviceconfigdict['mon'] = values[4]
	deviceconfigdict['day'] = values[5]
	deviceconfigdict['hour'] = values[6]
	deviceconfigdict['result'] = values[7][:2]
	if values[7][:2] == 'A':
		deviceconfigdict['score'] = 100
	elif values[7][:2] == 'A-':
		deviceconfigdict['score'] = 90
	elif values[7][:2] == 'B+':
		deviceconfigdict['score'] = 85
	elif values[7][:2] == 'B':
		deviceconfigdict['score'] = 80
	elif values[7][:2] == 'B-':
		deviceconfigdict['score'] = 75
	elif values[7][:2] == 'C+':
		deviceconfigdict['score'] = 70
	elif values[7][:2] == 'C':
		deviceconfigdict['score'] = 65
	elif values[7][:2] == 'C-':
		deviceconfigdict['score'] = 60
	elif values[7][:2] == 'D+':
		deviceconfigdict['score'] = 55
	elif values[7][:2] == 'D':
		deviceconfigdict['score'] = 50
	elif values[7][:2] == 'D-':
		deviceconfigdict['score'] = 45
	elif values[7][:2] == 'E+':
		deviceconfigdict['score'] = 40
	elif values[7][:2] == 'E':
		deviceconfigdict['score'] = 35
	elif values[7][:2] == 'E-':
		deviceconfigdict['score'] = 30
	else:
		deviceconfigdict['score'] = 80
	return deviceconfigdict

if __name__ == '__main__':
	dict = filetodict('/python/cgi-bin/checked/BJRack01_v10_sec-zzy_2016_2_19_12_A-.txt')
	print(dict)
#	print(dict.keys())
#	for (device, config) in dict.items():
#		print('*' * 30 + device + '*' * 30)
#		print(config)
#	print(dict['score'])

